package cn.jimmie.learn.viewArt.refreshview.core.recyclerview;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;


/**
 * Recycler 中添加
 */
class RecyclerFooterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_FOOTER = 0x222222;

    private View mFootView;

    private RecyclerView.Adapter mInnerAdapter;

    RecyclerFooterAdapter(RecyclerView.Adapter adapter) {
        mInnerAdapter = adapter;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER && mFootView != null) {
            return new RecyclerViewHolder(mFootView);
        }
        return mInnerAdapter.onCreateViewHolder(parent, viewType);
    }

    @Override
    public int getItemViewType(int position) {
        if (isFooterViewPos(position)) {
            return TYPE_FOOTER;
        }
        return mInnerAdapter.getItemViewType(position);
    }

    private int getRealItemCount() {
        return mInnerAdapter.getItemCount();
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (isFooterViewPos(position)) {
            return;
        }
        //noinspection unchecked
        mInnerAdapter.onBindViewHolder(holder, position);
    }

    @Override
    public int getItemCount() {
        return 1 + getRealItemCount();
    }

    private boolean isFooterViewPos(int position) {
        return position >= getRealItemCount();
    }

    void setFootView(View view) {
        mFootView = view;
        notifyDataSetChanged();
    }
}
